extends Node
class_name VFXManager

@onready var camera = get_node("/root/Main/CameraController/Camera3D")

func spawn_damage_number(position: Vector3, damage: float, is_crit: bool = false, is_heal: bool = false):
	var damage_number = Label3D.new()
	damage_number.position = position + Vector3(0, 2, 0)
	damage_number.billboard = BaseMaterial3D.BILLBOARD_ENABLED
	
	if is_heal:
		damage_number.text = "+" + str(int(damage))
		damage_number.modulate = Color(0.2, 1.0, 0.2)
		damage_number.outline_modulate = Color(0, 0.5, 0)
	elif is_crit:
		damage_number.text = str(int(damage)) + "!"
		damage_number.modulate = Color(1.0, 0.8, 0)
		damage_number.outline_modulate = Color(0.8, 0.2, 0)
		damage_number.font_size = 24
	else:
		damage_number.text = str(int(damage))
		damage_number.modulate = Color(1.0, 1.0, 1.0)
		damage_number.outline_modulate = Color(0.2, 0.2, 0.2)
	
	damage_number.outline_size = 8
	get_tree().root.add_child(damage_number)
	
	# Animate the damage number
	var tween = get_tree().create_tween()
	tween.set_parallel(true)
	tween.tween_property(damage_number, "position:y", position.y + 4, 1.0)
	tween.tween_property(damage_number, "modulate:a", 0.0, 1.0).set_ease(Tween.EASE_IN)
	tween.chain().tween_callback(damage_number.queue_free)

func spawn_hit_effect(position: Vector3, color: Color = Color.WHITE):
	var particles = GPUParticles3D.new()
	particles.position = position
	particles.emitting = true
	particles.amount = 10
	particles.lifetime = 0.5
	particles.one_shot = true
	
	var process_material = ParticleProcessMaterial.new()
	process_material.direction = Vector3(0, 1, 0)
	process_material.initial_velocity_min = 2.0
	process_material.initial_velocity_max = 4.0
	process_material.angular_velocity_min = -180.0
	process_material.angular_velocity_max = 180.0
	process_material.spread = 45.0
	process_material.gravity = Vector3(0, -10, 0)
	process_material.scale_min = 0.1
	process_material.scale_max = 0.3
	
	particles.process_material = process_material
	particles.draw_pass_1 = SphereMesh.new()
	particles.draw_pass_1.radial_segments = 4
	particles.draw_pass_1.rings = 2
	
	var material = StandardMaterial3D.new()
	material.albedo_color = color
	material.emission_enabled = true
	material.emission = color
	material.emission_energy = 2.0
	particles.material_override = material
	
	get_tree().root.add_child(particles)
	
	# Clean up after emission
	await get_tree().create_timer(2.0).timeout
	particles.queue_free()

func spawn_ability_effect(ability_name: String, position: Vector3):
	match ability_name:
		"Fireball":
			spawn_fireball_effect(position)
		"Shield Bash":
			spawn_shield_bash_effect(position)
		"Shadow Strike":
			spawn_shadow_strike_effect(position)
		"Rapid Fire":
			spawn_rapid_fire_effect(position)
		"Healing Wave":
			spawn_healing_wave_effect(position)

func spawn_fireball_effect(position: Vector3):
	var particles = GPUParticles3D.new()
	particles.position = position
	particles.emitting = true
	particles.amount = 50
	particles.lifetime = 1.0
	particles.one_shot = true
	
	var process_material = ParticleProcessMaterial.new()
	process_material.emission_shape = ParticleProcessMaterial.EMISSION_SHAPE_SPHERE
	process_material.emission_sphere_radius = 2.0
	process_material.direction = Vector3(0, 1, 0)
	process_material.initial_velocity_min = 1.0
	process_material.initial_velocity_max = 3.0
	process_material.spread = 180.0
	process_material.gravity = Vector3(0, 2, 0)
	process_material.scale_min = 0.2
	process_material.scale_max = 0.5
	
	particles.process_material = process_material
	particles.draw_pass_1 = SphereMesh.new()
	
	var material = StandardMaterial3D.new()
	material.albedo_color = Color(1.0, 0.4, 0.0)
	material.emission_enabled = true
	material.emission = Color(1.0, 0.6, 0.0)
	material.emission_energy = 3.0
	particles.material_override = material
	
	get_tree().root.add_child(particles)
	
	await get_tree().create_timer(2.0).timeout
	particles.queue_free()

func spawn_shield_bash_effect(position: Vector3):
	var ring = CSGTorus3D.new()
	ring.position = position + Vector3(0, 1, 0)
	ring.inner_radius = 0.5
	ring.outer_radius = 1.0
	ring.ring_sides = 8
	ring.sides = 16
	
	var material = StandardMaterial3D.new()
	material.albedo_color = Color(0.5, 0.5, 1.0, 0.5)
	material.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
	material.emission_enabled = true
	material.emission = Color(0.5, 0.5, 1.0)
	material.emission_energy = 2.0
	ring.material = material
	
	get_tree().root.add_child(ring)
	
	var tween = get_tree().create_tween()
	tween.set_parallel(true)
	tween.tween_property(ring, "scale", Vector3(2, 2, 2), 0.3)
	tween.tween_property(material, "albedo_color:a", 0.0, 0.3)
	tween.chain().tween_callback(ring.queue_free)

func spawn_shadow_strike_effect(position: Vector3):
	for i in range(5):
		var shadow = CSGBox3D.new()
		shadow.position = position + Vector3(randf() * 2 - 1, 0, randf() * 2 - 1)
		shadow.size = Vector3(0.1, 2, 0.1)
		
		var material = StandardMaterial3D.new()
		material.albedo_color = Color(0.5, 0.0, 0.5, 0.7)
		material.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
		shadow.material = material
		
		get_tree().root.add_child(shadow)
		
		var tween = get_tree().create_tween()
		tween.tween_property(shadow, "position:y", position.y + 2, 0.5)
		tween.parallel().tween_property(material, "albedo_color:a", 0.0, 0.5)
		tween.tween_callback(shadow.queue_free)
		
		await get_tree().create_timer(0.1).timeout

func spawn_rapid_fire_effect(position: Vector3):
	for i in range(3):
		var bullet = CSGSphere3D.new()
		bullet.position = position + Vector3(0, 1, 0)
		bullet.radius = 0.1
		
		var material = StandardMaterial3D.new()
		material.albedo_color = Color(1.0, 1.0, 0.0)
		material.emission_enabled = true
		material.emission = Color(1.0, 1.0, 0.0)
		material.emission_energy = 2.0
		bullet.material = material
		
		get_tree().root.add_child(bullet)
		
		var direction = Vector3(randf() * 2 - 1, 0, randf() * 2 - 1).normalized()
		var tween = get_tree().create_tween()
		tween.tween_property(bullet, "position", position + direction * 5, 0.3)
		tween.parallel().tween_property(bullet, "scale", Vector3.ZERO, 0.3)
		tween.tween_callback(bullet.queue_free)
		
		await get_tree().create_timer(0.1).timeout

func spawn_healing_wave_effect(position: Vector3):
	var particles = GPUParticles3D.new()
	particles.position = position
	particles.emitting = true
	particles.amount = 30
	particles.lifetime = 1.0
	particles.one_shot = true
	
	var process_material = ParticleProcessMaterial.new()
	process_material.direction = Vector3(0, 1, 0)
	process_material.initial_velocity_min = 1.0
	process_material.initial_velocity_max = 2.0
	process_material.spread = 30.0
	process_material.gravity = Vector3(0, -2, 0)
	process_material.scale_min = 0.1
	process_material.scale_max = 0.2
	
	particles.process_material = process_material
	particles.draw_pass_1 = SphereMesh.new()
	
	var material = StandardMaterial3D.new()
	material.albedo_color = Color(0.0, 1.0, 0.0)
	material.emission_enabled = true
	material.emission = Color(0.0, 1.0, 0.0)
	material.emission_energy = 2.0
	particles.material_override = material
	
	get_tree().root.add_child(particles)
	
	await get_tree().create_timer(2.0).timeout
	particles.queue_free()